package Eyjafjalla.patch;

import Eyjafjalla.card.power.DancingLava_Eyjafjalla;
import Eyjafjalla.event.ALetter;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePostfixPatch;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractEvent;
import com.megacrit.cardcrawl.random.Random;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rewards.RewardItem;
import com.megacrit.cardcrawl.screens.CombatRewardScreen;

public class PompeiiPatch {
    public static boolean facePompeii = false;
    public static int eventPercent = 10;
    public static boolean enterEvent = false;
    public static int rewardLevel = -1;

    public static void reset(){
        facePompeii = false;
        enterEvent = false;
        rewardLevel = -1;
        eventPercent = 10;
    }

    @SpirePatch(clz = AbstractDungeon.class,method = "generateEvent")
    public static class EventGenPatch{
        @SpirePostfixPatch
        public static AbstractEvent Postfix(AbstractEvent _ret, Random rng){
            if(!PompeiiPatch.enterEvent&&!(_ret instanceof ALetter)){
                Random duplicate = rng.copy();
                if(eventPercent>duplicate.random(99)){
                    eventPercent = -1000;
                    return new ALetter();
                }
                else{
                    eventPercent+=10;
                }
            }
            else if(PompeiiPatch.enterEvent&&(_ret instanceof ALetter)){
                return AbstractDungeon.generateEvent(rng);
            }
            return _ret;
        }
    }

    @SpirePatch(clz = CombatRewardScreen.class,method = "setupItemReward")
    public static class SetUpRewardPatch{
        @SpirePostfixPatch
        public static void Postfix(CombatRewardScreen _inst){
            if(rewardLevel==AbstractDungeon.floorNum){
                RewardItem card = new RewardItem();
                card.cards.clear();;
                card.cards.add(new DancingLava_Eyjafjalla());
                _inst.rewards.add(card);
                _inst.rewards.add(new RewardItem(AbstractDungeon.returnRandomNonCampfireRelic(AbstractRelic.RelicTier.COMMON)));
                _inst.rewards.add(new RewardItem(AbstractDungeon.returnRandomNonCampfireRelic(AbstractRelic.RelicTier.UNCOMMON)));
                _inst.positionRewards();
            }
            else {
                rewardLevel = -1;
            }
        }
    }

}
